package com.road.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.road.mapper.SysLogMapper;
import com.road.model.easyui.PageInfo;
import com.road.model.entity.SysLog;
import com.road.service.LogService;

@Service
public class LogServiceImpl implements LogService {

	@Autowired
	private SysLogMapper sysLogMapper;

	private static final Logger LOGGER = LoggerFactory.getLogger(LogServiceImpl.class);

	private List<SysLog> syslogs = new CopyOnWriteArrayList<>();

	@Override
	@Scheduled(cron = "0 0/10 * * * ?")
	public void insertLog() {
		LOGGER.debug("保存操作日志中。。。");
		if (syslogs.size() > 0) {
			List<SysLog> logs = new ArrayList<>();
			synchronized (LogServiceImpl.class) {
				logs.addAll(syslogs);
				syslogs.clear();
			}
			for (SysLog sysLog : logs) {
				sysLogMapper.insert(sysLog);
			}
		}
	}

	@Override
	public void findDataGrid(PageInfo<SysLog> pageInfo) {
		pageInfo.setRows(sysLogMapper.findDataGrid(pageInfo));
		pageInfo.setTotal(sysLogMapper.findDataGridCount(pageInfo));
	}

	@Override
	public void save(SysLog log) {
		syslogs.add(log);
	}
}
